package com.decent.ejfadmin.apply.service.impl;

import com.decent.ejfadmin.apply.dao.RebateApplyDao;
import com.decent.ejfadmin.apply.request.RebateApplyQueryParam;
import com.decent.ejfadmin.apply.service.RebateApplyService;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.enums.ErrorCode;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.common.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author zhangchao
 * @date 2018/7/30 15:32
 */
@Service
public class RebateApplyServiceImpl implements RebateApplyService {

    @Autowired
    private RebateApplyDao rebateApplyDao;

    /**
     * 推荐有礼申请查询分页数据
     *
     * @param param
     * @return
     */
    @Override
    public Page findRebateApplyPage(RebateApplyQueryParam param) {
        int total = rebateApplyDao.queryRebateApplyCount(param);
        param.getPageUnit().setTotal(total);
        if (total == 0) {
            param.getPageUnit().setResultList(new ArrayList<>());
        } else {
            param.getPageUnit().setResultList(rebateApplyDao.queryRebateApplyPage(param));
        }
        return param.getPageUnit();
    }

    @Override
    public void changeState(Long applyId, Integer state) throws SupException {
        boolean flag = (null == applyId || state == null || (! Constants.CONNECTED.equals(state) &&  ! Constants.NO_CONNECT.equals(state)));
        if (flag) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误 ");
        }
        rebateApplyDao.changeState(applyId,state);
    }

    @Override
    public void addRemark(Long applyId, String remark) throws SupException {
        if (null == applyId || StringUtils.isBlank(remark)) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误 ");
        }
        rebateApplyDao.addRemark(applyId,remark);
    }
}
